

'use strict';

const Controller = require('egg').Controller;

class DocController extends Controller {
    async detail() {
        try {
            const {id} = this.ctx.request.query
            const result = await this.service.doc.detail(id)
            if (result.code === 1) {
                this.ctx.response.body = {
                    code: 1,
                    data: result.data
                }
            } else {
                this.ctx.response.body = {
                    code: 0,
                    msg: result.msg
                }
            }

        }catch(e) {
            this.ctx.response.body = {
                code: 0,
                msg: e.message
            }
        }
    }
    async deleteRow() {
        try {
            const { phone, id } = this.ctx.request.body
            const reuslt = await this.ctx.service.doc.delete({
                phone, id
            })
            if (reuslt.code === 1) {
                this.ctx.response.body = {
                    code: 1,
                    msg: '删除成功'
                }
            } else {
                this.ctx.response.body = {
                    code: 0,
                    msg:result.msg
                }
            }

        } catch(e) {
            this.ctx.response.body = {
                code: 0, 
                msg: e.message
            }
        }
    }
    async list() {
        // 获取所有的文档
        try{
            const { phone } = this.ctx.request.body
            const result = await this.ctx.service.doc.list(phone)
            if (result.code ===1) {
                this.ctx.response.body = {
                    code: 1,
                    data: result.data
                }
            } else {
                this.ctx.response.body = {
                    code: 0,
                    msg: result.msg
                }
            }
        } catch (e) {
            this.ctx.response.body = {
                code: 0,
                msg: e.message
            }
        }
    }
  async add() {
      try {
        // 添加文档
        const { phone, text, type, name } = this.ctx.request.body

        const result = await this.ctx.service.doc.add({
            phone, text, type, name
        })

        // 如果返回的结果code为1，代表文档添加成功
        if (result.code === 1) {
            this.ctx.response.body = {
                code: 1,
                msg: '添加成功'
            }
        } else {
            this.ctx.response.body = {
                code: 0,
                msg: '添加失败'
            }
        }

      } catch(e) {
          this.ctx.response.body = {
              code: 0,
              msg: e.message
          }
      }
      
    
  }
}

module.exports = DocController;
